#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , worker(new Worker())
{
    ui->setupUi(this);
    connect(this,&Widget::signAutoConnection,worker
            ,&Worker::doWork,Qt::ConnectionType::AutoConnection);
    connect(this,&Widget::signDirectConnection,worker
            ,&Worker::doWork,Qt::ConnectionType::DirectConnection);
    connect(this,&Widget::signQueuedConnection,worker
            ,&Worker::doWork,Qt::ConnectionType::QueuedConnection);
    connect(this,&Widget::signBlockingQueuedConnection,worker
            ,&Worker::doWork,Qt::ConnectionType::BlockingQueuedConnection);
    thread.start();
    worker->moveToThread(&thread);
}


Widget::~Widget()
{
    thread.exit();
    thread.wait();
    delete ui;
}


void Widget::on_btnAutoConnection_clicked()
{
    emit this->signAutoConnection("AutoConnection");
    qDebug() << "on_btnAutoConnection_clicked master ThreadId="
<< QThread::currentThreadId();
}

void Widget::on_btnDirectConnection_clicked()
{
    emit this->signDirectConnection("DirectConnection");
    qDebug() << "on_btnDirectConnection_clicked master ThreadId="
 << QThread::currentThreadId();
}

void Widget::on_btnQueuedConnection_clicked()
{
    emit this->signQueuedConnection("QueuedConnection");
    qDebug() << "on_btnQueuedConnection_clicked master ThreadId="
<< QThread::currentThreadId();
}

void Widget::on_btnBlockingQueuedConnection_clicked()
{
    emit this->signBlockingQueuedConnection("BlockingQueuedConnection");
    qDebug() << "on_btnBlockingQueuedConnection_clicked master ThreadId="
<< QThread::currentThreadId();
}

void Widget::on_btnUniqueConnection_clicked()
{
    //默认同一个信号和槽可以被多次连接，信号触发时，槽会执行多次。
    Qt::ConnectionType tp = Qt::ConnectionType::AutoConnection;

    //取消下面两行的注释，添加UniqueConnection,同一个信号和槽可以连接一次，
    //信号触发时，槽会执行一次
       tp = Qt::ConnectionType(Qt::ConnectionType::AutoConnection
         | Qt::ConnectionType::UniqueConnection);

    connect(this,&Widget::signUniqueConnection,worker
            ,&Worker::doWork
            ,tp);

    connect(this,&Widget::signUniqueConnection,worker
            ,&Worker::doWork
            ,tp);

        emit this->signUniqueConnection("UniqueConnection");
    //    qDebug() << "";
}

